﻿using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Util;
using Model.Const;
using Repository.Util;
using Xproject.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Model.Util.DTO;


namespace Services.Util
{
   public class UserCollectionServices : Service
    {
        IRepositoryAsync<UserCollection> UserCollectionRepository;
        public UserCollectionServices(IDataContextAsync dataContext) : base(dataContext)
        {
            UserCollectionRepository = m_UnitOfWork.RepositoryAsync<UserCollection>();
        }

        public List<UserCollection> GetMyCollections(int? offset, int? limit, string userId)
        {
            return UserCollectionRepository.GetMyCollections(offset.Value, limit.Value, userId);
        }

        public void AddUserCollectionContent( string UserId, string RefId, string refUrl, string CollectDesc,string remarks,string imgUrl)
        {           
            UserCollection col = new UserCollection();
            col.CreateTime = DateTime.Now;
            col.CollectDesc = CollectDesc;
            col.CollectionId = Guid.NewGuid().ToString();
            col.CollectUrl = refUrl;
            col.ImgUrl = imgUrl;
            col.RefId = RefId;
            col.Remarks = remarks;
            col.UserId = UserId;
            col.EntityState = EntityState.Added;
            UserCollectionRepository.Insert(col);
        }
        public bool AddUserCollection (string UserId, string RefId,  string refUrl, string CollectDesc, string remarks, string imgUrl,ref string message)
        {
            if (UserCollectionRepository.GetUserCollectionByRefId(UserId, RefId) != null)
            {
                message = "收藏夹已经有此宝贝啦";
                return false;
            }
            AddUserCollectionContent(UserId,RefId,refUrl,CollectDesc,remarks,imgUrl);
            return m_UnitOfWork.SaveChanges() == 1;
        }

    }
}
